$(function(){
	// 加载动画
	var login = $('#login');
	var height = $(window).height();
	if (height > 446) {
		var ph = (height - 446) / 2;
		login.animate({top: ph + 'px'}, 500);
	} else {
		login.css('margin-bottom', '30px').animate({top: '30px'}, 500);
	}
	// 表单处理
	var username = $('#username');
	var password = $('#password');
	var submit = $('#submit');
	var error_value = $('#error_value');
	var requesting = false;
	var input_event = function(dom) {
		dom.bind('focus', function() {
			$(this).parent().addClass('focus');
		});
		dom.bind('blur', function() {
			$(this).parent().removeClass('focus');
		});
	};
	input_event(username);
	input_event(password);
	document.onkeydown = function(e) {
		var ev = document.all ? window.event : e;
		if (ev.keyCode == 13) submit.trigger('click');
	}
	submit.bind('click', function() {
		var t = $(this).find('span');
		var error_tip = function(val) {
			error_value.text(val).parent().slideDown();
			setTimeout(function() {
				error_value.parent().slideUp(function() {
					t.fadeOut(function() {
						$(this).text('登录').fadeIn(function() {
							requesting = false;
						});
					});
				});
			}, 2000);
		};
		if (requesting) return false;
		requesting = true;
		t.fadeOut(function() {
			$(this).text('登录中...').fadeIn(function() {
				var param = {
					userName:$.trim(username.val()),
					password:$.md5(password.val())
				};
				$.ajax({
					url: '/lottery/rest/login',
					type: 'POST',
					cache: false,
					dataType: 'html',
					data: param,
					error: function() {
						error_tip('请求失败，请重试');
					},
					success: function(res) {
						var res = JSON.parse(res);
						var code = res.status;
						var msg = res.errorMsg;
						if (code == '00') {
							//error_tip(decodeURIComponent(error_message));
							window.location.href = '/lottery/rest/index';
						} else {
							error_tip(decodeURIComponent(msg));
						}
					},
				});
			});
		});
	});
});